package com.example.javaweb_project.service;

import com.example.javaweb_project.dao.EmployeeDao;
import com.example.javaweb_project.model.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class EmployeeService {
    @Autowired
    private EmployeeDao employeeDao;
    public List<Employee> list(Employee employee){
        List<Employee> sts;
        if(employee.getName()==""&&employee.getAge()!=null){
            sts = employeeDao.findByAge(employee.getAge());
        }
        else{
            sts = employeeDao.findAll(Example.of(employee));
        }
        return sts;
    }
    public void deleteById(Long id) {
        employeeDao.deleteById(id);
    }

    /*
    sava函数，有则更新，无则插入
     */
    public void save(Employee st) {
        employeeDao.save(st);
    }
    public Employee getbyid(Long id) {
        Employee st;
        try{
            st =  employeeDao.getReferenceById(id);
        }
        catch (Exception e){
            st = null;
        }
        return st;
    }
}
